package com.weight.system.equmt.web;

import com.weight.system.equmt.PO.EqumtInfo;
import com.weight.system.equmt.VO.EqumtInfoVO;
import com.weight.system.equmt.service.EqumtInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 设备信息表
 */
@RestController
@Slf4j
@RequestMapping("/equmt/equmtInfo")
public class EqumtInfoController {

	@Autowired
	private EqumtInfoService equmtInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询设备信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForEqumtInfoList",method=RequestMethod.GET)
	public ModelAndView queryForEqumtInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/equmt/queryForEqumtInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询设备信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForEqumtInfoListDG",method=RequestMethod.GET)
	public String queryForEqumtInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtName = request.getParameter("equmtName");
			String equmtIp = request.getParameter("equmtIp");
			String orgId = request.getParameter("orgId");
			String fixCycle = request.getParameter("fixCycle");
			String equmtStatue = request.getParameter("equmtStatue");
			String equpicPath = request.getParameter("equpicPath");
			String equmtOverload = request.getParameter("equmtOverload");
			String equmtDiva = request.getParameter("equmtDiva");
			String equmtDivb = request.getParameter("equmtDivb");
			String equmtDivc = request.getParameter("equmtDivc");
			String equmtDivd = request.getParameter("equmtDivd");
			String equmtDive = request.getParameter("equmtDive");
			String equmtLon = request.getParameter("equmtLon");
			String equmtLat = request.getParameter("equmtLat");
			String takecareStdate = request.getParameter("takecareStdate");
			String takecareCudate = request.getParameter("takecareCudate");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtName", equmtName);
			map.put("equmtIp", equmtIp);
			map.put("orgId", orgId);
			map.put("fixCycle", fixCycle);
			map.put("equmtStatue", equmtStatue);
			map.put("equpicPath", equpicPath);
			map.put("equmtOverload", equmtOverload);
			map.put("equmtDiva", equmtDiva);
			map.put("equmtDivb", equmtDivb);
			map.put("equmtDivc", equmtDivc);
			map.put("equmtDivd", equmtDivd);
			map.put("equmtDive", equmtDive);
			map.put("equmtLon", equmtLon);
			map.put("equmtLat", equmtLat);
			map.put("takecareStdate", DateFormUtils.getStringToDate(takecareStdate, pattern));
			map.put("takecareCudate", DateFormUtils.getStringToDate(takecareCudate, pattern));

			
			//2、查询设备信息表列表
			List<EqumtInfo> equmtInfoList = equmtInfoService.findEqumtInfoByPage(page,rows,map);
			log.info("equmtInfoList={}",equmtInfoList);
			List<EqumtInfoVO> equmtInfoVOList = equmtInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<EqumtInfoVO> result = new PageResult<EqumtInfoVO>(equmtInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param equmtInfoVO
	 * @param equmtInfo
	 * @return
	 */
	public void changeVOToPO(EqumtInfoVO equmtInfoVO,EqumtInfo equmtInfo){
			BeanUtils.copyProperties(equmtInfoVO,equmtInfo);
			equmtInfo.setTakecareStdate(DateFormUtils.getStringToDate(equmtInfoVO.getTakecareStdate(), pattern));
			equmtInfo.setTakecareCudate(DateFormUtils.getStringToDate(equmtInfoVO.getTakecareCudate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param equmtInfoVO
	 * @param equmtInfo
	 * @return
	 */
	public void changePoToVO(EqumtInfoVO equmtInfoVO,EqumtInfo equmtInfo){
			BeanUtils.copyProperties(equmtInfo,equmtInfoVO);
			equmtInfoVO.setTakecareStdate(DateFormUtils.getDateToString(equmtInfo.getTakecareStdate(), pattern));
			equmtInfoVO.setTakecareCudate(DateFormUtils.getDateToString(equmtInfo.getTakecareCudate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param equmtInfo
	 * @return
	 */
	public EqumtInfoVO changePoToVO(EqumtInfo equmtInfo){
			EqumtInfoVO equmtInfoVO=new EqumtInfoVO();
			BeanUtils.copyProperties(equmtInfo,equmtInfoVO);
			equmtInfoVO.setTakecareStdate(DateFormUtils.getDateToString(equmtInfo.getTakecareStdate(), pattern));
			equmtInfoVO.setTakecareCudate(DateFormUtils.getDateToString(equmtInfo.getTakecareCudate(), pattern));
			

			return equmtInfoVO;
	}

	/**
	 * 3.查询设备信息表--详情页
	 * @param equmtInfoId
	 * @return
	 */
	@RequestMapping("/queryForEqumtInfoDetail")
	public ModelAndView queryForEqumtInfoDetail(@RequestParam(value="equmtInfoId",required=false) String equmtInfoId){
			//1.校验id
			CommonUtils.validateExistID(equmtInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("equmtInfoId", equmtInfoId);
			log.info("查询设备信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			EqumtInfo equmtInfo = equmtInfoService.findByEqumtInfoId(map);
			
			EqumtInfoVO equmtInfoVO = changePoToVO(equmtInfo);
			//3、返回前台
			log.info("查询设备信息表查询成功：equmtInfo={}",equmtInfo);
			modelAndView.setViewName("/system/equmt/queryForEqumtInfoDetail");
			modelAndView.addObject("equmtInfoVO",equmtInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改设备信息表操作
	 * @param equmtInfoId
	 * @return
	 */
	@RequestMapping(value="/addEqumtInfo")
	public ModelAndView addEqumtInfo(@RequestParam(value="equmtInfoId",required=false) String equmtInfoId){
			//1、创建返回对象
			EqumtInfoVO equmtInfoVO = new EqumtInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(equmtInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("equmtInfoId", equmtInfoId);
				log.info("修改操作，查询ID：equmtInfoId={}",equmtInfoId);
				EqumtInfo equmtInfo = equmtInfoService.findByEqumtInfoId(map);
				this.changePoToVO(equmtInfoVO,equmtInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/equmt/addEqumtInfo");
			modelAndView.addObject("equmtInfoVO",equmtInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改设备信息表操作
	 * @param equmtInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveEqumtInfo")
	public JsonResultUtil saveEqumtInfo(@Valid EqumtInfoVO equmtInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,equmtInfoVO);
			
			//2.获取id
			String equmtInfoId=equmtInfoVO.getEqumtInfoId();
			EqumtInfo equmtInfo=new EqumtInfo();
			BeanUtils.copyProperties(equmtInfoVO,equmtInfo);
			this.changeVOToPO(equmtInfoVO,equmtInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(equmtInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("equmtInfoId", equmtInfoId);
				log.info("修改操作，查询ID：equmtInfoId={}",equmtInfoId);
				//查询是否存在
				equmtInfoService.findByEqumtInfoId(map);
				try{
					equmtInfo.setUpdateBy("000");
					equmtInfoService.updateEqumtInfo(equmtInfo);
					log.info("更新操作成功：equmtInfo={}",equmtInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					equmtInfo.setEqumtInfoId(CommonUtils.getUUId());
					equmtInfo.setCreateBy("000");
					equmtInfo.setUpdateBy("000");
					equmtInfoService.insertEqumtInfo(equmtInfo);
					log.info("添加操作成功：equmtInfo={}",equmtInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除设备信息表操作
	 * @param equmtInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByEqumtInfoId")
	public JsonResultUtil deleteByEqumtInfoId(@RequestParam(value="equmtInfoId",required=false) String equmtInfoId){
			//1.校验id
			CommonUtils.validateExistID(equmtInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("equmtInfoId", equmtInfoId);
			equmtInfoService.findByEqumtInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除设备信息表入参，map={}",map);
			
			try{
				equmtInfoService.upDelByEqumtInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除设备信息表操作---批量删除
	 * @param equmtInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByEqumtInfoIdBatch")
	public JsonResultUtil deleteByEqumtInfoIdBatch(@RequestParam(value="equmtInfoIdList",required=false) List equmtInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(equmtInfoIdList);
			
			//2.校验数据库
			equmtInfoService.findByEqumtInfoIdList(equmtInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("equmtInfoIdList", equmtInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除设备信息表操作入参，map={}",map);
			
			try{
				equmtInfoService.upDelByEqumtInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}